#!/usr/bin/env python
# -*- coding:utf-8 -*-


# 饼图实现
from matplotlib import font_manager as fm 
import matplotlib as mpl 
import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib import cm  

# plt.style.use('ggplot')
# plt.show()


# 将实际数据应用于官方Demo
# 原始数据
shapes = ['Cross','Cone','Egg','Teardrop','Chevron','Diamond','Cylinder','Rectangle', \
        'Flash','Cigar','Changing','Formation','Oval','Disk', \
            'Sphere','Fireball','Triangle','Circle','Light']
values = [287,383,842,866,1187,1405,1495,1620,1717,2313,2378,3070,4332,5841,6482,7785,9358,9818,20254]

s = pd.Series(values,index=shapes)
print(s)


"""
from matplotlib import font_manager as fm 
import matplotlib as mpl 

labels = s.index
sizes = s.values 
explode = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) 

fig1,ax1 = plt.subplots()
patches,texts, autotexts = ax1.pie(sizes,explode=explode,labels=labels,autopct='%1.0f%%',shadow=False,startangle=170)
ax1.axis('equal')

plt.savefig('F:\\PythonProject\\AI\\Python\\DataAnalysis\\scripts\\files\\Demo_project.png')
plt.show()
# 图的一些效果问题: 颜色生硬,部分文字挤在一起,绘图显示不齐整.
"""


# 1.3 改善措施
"""
重新设置字体大小
设置自选颜色
设置图例
将某些类别突出显示
"""
# 1.3.1 重新设置字体大小
"""
from matplotlib import font_manager as fm 
import matplotlib as mpl 

labels = s.index
sizes = s.values 
explode = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)

fig1,ax1 = plt.subplots()
patches,texts, autotexts = ax1.pie(sizes,explode=explode, labels=labels,autopct='%1.0f%%',shadow=False,startangle=170)
ax1.axis('equal')

# 重新设置字体大小
proptease = fm.FontProperties()
proptease.set_size('xx-small')
plt.setp(autotexts,fontproperties=proptease)
plt.setp(texts,fontproperties=proptease)

plt.savefig('F:\\PythonProject\\AI\\Python\\DataAnalysis\\scripts\\files\\Demo_project_set_font.png')
plt.show()
"""



# 1.3.2 设置显示颜色, Method 1
"""
labels = s.index 
sizes = s.values 
explode = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)

fig1,ax1 = plt.subplots(figsize=(6,6)) # 设置绘图区域大小

a = np.random.rand(1,19)
color_vals = list(a[0])
my_norm = mpl.colors.Normalize(-1,1) # 将颜色数据的范围设置为[0,1]
my_cmap = mpl.cm.get_cmap('rainbow',len(color_vals)) # 可选择合适的colormap,如rainbow

patches,texts,autotexts = ax1.pie(sizes,explode=explode,labels=labels,autopct='%1.0f%%',shadow=False,startangle=170,colors=my_cmap(my_norm(color_vals)))
ax1.axis('equal')

# 重新设置字体大小
proptease = fm.FontProperties()
proptease.set_size('xx-small')
# font size include: 'xx-small','x-small','small','medium','large','x-large','xx-large', or number, e.g. '12'
plt.setp(autotexts,fontproperties=proptease)
plt.setp(texts,fontproperties=proptease)

plt.savefig('F:\\PythonProject\\AI\\Python\\DataAnalysis\\scripts\\files\\Demo_project_set_color_1.png')
plt.show()
'''
这种方法设置颜色时,类别比较多时,部分颜色的填充会重复.
'''
"""





# 1.3.3 设置显示颜色, Method 2:
"""
from matplotlib import cm  

labels = s.index 
sizes = s.values 

fig,ax = plt.subplots(figsize=(6,6)) # 设置绘图区域大小。
colors = cm.rainbow(np.arange(len(sizes))/len(sizes)) # colormaps: Paired,autumn,rainbow,gray,spring,Darks.
patches, texts, autotexts = ax.pie(sizes,labels=labels,autopct='%1.0f%%',shadow=False,startangle=170,colors=colors)

ax.axis('equal')
ax.set_title('Shapes -------',loc='left')

# 重新设置字体大小
proptease = fm.FontProperties()
proptease.set_size('xx-small')
# font size include: 'xx-small','x-small','small','medium','large','x-large','xx-large', or number, e.g. '12'
plt.setp(autotexts,fontproperties=proptease)
plt.setp(texts,fontproperties=proptease)

plt.savefig('F:\\PythonProject\\AI\\Python\\DataAnalysis\\scripts\\files\\Demo_project_set_color_2.png')
plt.show()
# 从图中可以看出颜色显示是连续的.
"""



# 1.3.4 设置图例(legend)
"""
from matplotlib import font_manager as fm 
from matplotlib import cm  

labels = s.index
sizes = s.values

fig,ax = plt.subplots(figsize=(6,6)) # 设置绘图区域大小
colors = cm.rainbow(np.arange(len(sizes))/len(sizes)) # colormaps: Paired,autumn,rainbow,gray,spring,Darks.
patches, texts,autotexts = ax.pie(sizes,labels=labels,autopct='%1.0f%%',shadow=False,startangle=170,colors=colors)

ax.axis('equal')

# 重新设置字体大小
proptease = fm.FontProperties()
proptease.set_size('xx-small')
# font size include: 'xx-small','x-small','small','medium','large','x-large','xx-large', or number, e.g. '12'
plt.setp(autotexts,fontproperties=proptease)
plt.setp(texts, fontproperties=proptease)

ax.legend(labels,loc=2)
plt.savefig('F:\\PythonProject\\AI\\Python\\DataAnalysis\\scripts\\files\\Demo_project_set_legend_error.png')
plt.show()
# 当类别较多时,图例(legend)的位置摆放显示有重叠.需要进行调整.
"""




# 1.3.5 重新设置图例(legend)
"""
labels = s.index 
sizes = s.values 

fig,axes = plt.subplots(figsize=(10,5),ncols=2) # 设置绘图区域大小
ax1,ax2 = axes.ravel()
# colormaps: Paired,autumn,rainbow,gray,spring,Darks.
colors = cm.rainbow(np.arange(len(sizes))/len(sizes))
patches,texts,autotexts = ax1.pie(sizes,labels=labels,autopct='%1.0f%%',shadow=False,startangle=170,colors=colors) 

ax1.axis('equal')
# 重新设置字体大小
proptease = fm.FontProperties()
proptease.set_size('xx-small')
# font size include: 'xx-small','x-small','small','medium','large','x-large','xx-large', or number, e.g. '12'
plt.setp(autotexts, fontproperties=proptease)
plt.setp(texts, fontproperties=proptease)

ax1.set_title('Shapes',loc='center')

# ax2 只显示图例(legend)
ax2.axis('off')
ax2.legend(patches,labels,loc='center left')

plt.tight_layout()
plt.savefig('F:\\PythonProject\\AI\\Python\\DataAnalysis\\scripts\\files\\Demo_project_set_legend_good.png')
plt.show()
"""



# 1.3.6 将某些类别突出显示
'''
- 将某些类别突出显示
- 控制label的显示位置。
- 控制百分比的显示位置.
'''
# 控制突出位置的大小
labels = s.index
sizes = s.values 
explode = (0.1,0,0,0,0,0,0,0,0,0,0,0,0,0.2,0,0,0,0.1,0)

fig,axes = plt.subplots(figsize=(8,5),ncols=2) # 设置绘图区域大小
ax1,ax2 = axes.ravel()
# colormaps: Paired,autumn,rainbow,gray,spring,Darks.
colors = cm.rainbow(np.arange(len(sizes))/len(sizes))
patches,texts,autotexts = ax1.pie(sizes,labels=labels,autopct='%1.0f%%',explode=explode,shadow=False,startangle=170,colors=colors,labeldistance=1.2,pctdistance=1.03,radius=0.4)
# labeldistance: 控制labels显示的位置
# pctdistance: 控制百分比显示的位置.
# radius: 控制切片突出的距离.
ax1.axis('equal')

# 重新设置字体大小
proptease = fm.FontProperties()
proptease.set_size('xx-small')
# font size include: 'xx-small','x-small','small','medium','large','x-large','xx-large', or number, e.g. '12'
plt.setp(autotexts,fontproperties=proptease)
plt.setp(texts,fontproperties=proptease)

ax1.set_title('Shapes',loc='center')

# ax2 只显示图例(legend)
ax2.axis('off')
ax2.legend(patches,labels,loc='center left')

plt.tight_layout()
plt.savefig('F:\\PythonProject\\AI\\Python\\DataAnalysis\\scripts\\files\\Demo_project_final.png')
plt.show()

